package com.chefu.body.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chefu.body.mapper.CarDetailMapper;
import com.chefu.body.mapper.CarOwnerMapper;
import com.chefu.body.pojo.CarDetail;
import com.chefu.body.pojo.CarOwner;
import com.chefu.common.pojo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarOwnerService {

    @Autowired
    private CarDetailMapper carDetailMapper;

    @Autowired
    private CarOwnerMapper carOwnerMapper;

    /**
     * 分页查询车主信息
     *
     * @param page
     * @param pageSize
     * @param carOwner
     * @return
     */
    public PageResult<CarOwner> queryCarOwnerByPage(Integer page, Integer pageSize, CarOwner carOwner) {
        IPage<CarOwner> iPage = new Page<>();
        QueryWrapper<CarOwner> query = Wrappers.query();
        if (StringUtils.isNotEmpty(carOwner.getCarownerName())) {
            query.like("carowner_name", carOwner.getCarownerName());
        }
        if (StringUtils.isNotEmpty(carOwner.getPhone())) {
            query.eq("phone", carOwner.getPhone());
        }
        if (carOwner.getStatus() != null && !carOwner.getStatus().equals("")) {
            query.eq("status", carOwner.getStatus());
        }
        iPage = carOwnerMapper.selectPage(iPage, query);

        PageResult<CarOwner> pageResult = new PageResult<CarOwner>();
        pageResult.setTotalPage((int) iPage.getPages());
        pageResult.setTotal((int) iPage.getTotal());
        pageResult.setItems(iPage.getRecords());
        return pageResult;
    }

    /**
     * 根据车主id查询查询所属车辆详细信息
     *
     * @param carownerId
     * @return
     */
    public List<CarDetail> queryCarDetileByCarOwnerId(Long carownerId) {

        CarDetail carDetail = new CarDetail();
        if (carownerId != null && !carownerId.equals("")) {

            carDetail.setCarownerId(carownerId);
        }
        QueryWrapper<CarDetail> query = Wrappers.query(carDetail);

        List<CarDetail> carDetails = carDetailMapper.selectList(query);

        return carDetails;
    }
}

